﻿using AutoMapper;
using MediatR;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.WhitestripWeighing
{
    public class PartitionCreateHandler : IRequestHandler<PartitionCreateCommand, Resqults>
    {
        private readonly IBateRepository<Partition> _partitionRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="partitionRepository"></param>
        public PartitionCreateHandler(IBateRepository<Partition> partitionRepository)
        {
            _partitionRepository = partitionRepository;
        }

        /// <summary>
        /// 分割领用新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Resqults> Handle(PartitionCreateCommand request, CancellationToken cancellationToken)
        {
            var list = new Resqults() { Code = 0 };
            var query = new Partition()
            {
                ShoppingCodeId = request.ShoppingCodeId,
                ShopId = request.ShopId,
                RoughWeight = request.RoughWeight,
                PartitionTare = request.PartitionTare,
                NetWeight = request.NetWeight,
                PartitionNum = request.PartitionNum,
                PartitionRemarks = request.PartitionRemarks,
                PartitionDate = DateTime.Now,
                PartitionStart = request.PartitionStart,
            };
            var result = await _partitionRepository.Add(query);
            if (result > 0)
            {
                list.Code = 1;
                list.Messges = "新增成功";
            }

            return list;
        }
    }
}
